/**
 * 
 */
package com.mycompany.server;

import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mycompany.client.async.CalendarService;
import com.mycompany.client.model.CalendarBO;
import com.mycompany.server.config.CachePM;

/**
 * @author coral.ma
 *
 */
public class CalendarServiceImpl extends RemoteServiceServlet implements CalendarService {

	private static final long serialVersionUID = 7197459440914270200L;

	public CalendarBO addCalendar(CalendarBO calendarBO) {
		try {
			List<CalendarBO> calendarBOs = CachePM.getCalendars();
			calendarBO.setEventId(calendarBOs.size()+1);
			CachePM.getCalendars().add(calendarBO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendarBO;
	}

	public CalendarBO updateCalendar(CalendarBO calendarBO) {
		try {
			List<CalendarBO> calendarBOs = CachePM.getCalendars();
			for(CalendarBO bo : calendarBOs) {
				if(bo.getEventId() == calendarBO.getEventId()) {
					bo.setStartDate(calendarBO.getStartDate());
					bo.setEndDate(calendarBO.getEndDate());
					bo.setName(calendarBO.getName());
					bo.setDescription(calendarBO.getDescription());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendarBO;
	}

	public Boolean deleteCalendar(CalendarBO calendarBO) {
		List<CalendarBO> calendarBOs = CachePM.getCalendars();
		return calendarBOs.remove(calendarBO);
	}

	public List<CalendarBO> loadCalendars(String userID) {
		List<CalendarBO> calendars = CachePM.getCalendars();
		return calendars;
	}
}
